var express = require('express');
var router = express.Router();
var querySql=require('../db/index')
// md5的引入
const {PWD_SALT,PRIVATE_KEY,EXPIRESD} = require('../utils/constant')
// 引入md5的加密方法
const {md5,upload}=require('../utils/index')
// 引用jwt 登录成功后服务器使用签名密钥生成JWT，并返回JWT给客户端
const jwt = require('jsonwebtoken')

// const multer  = require('multer')
// const upload = multer({ dest: 'uploads/' })

router.post('/add', async(req, res, next) => {
  let {username,password,nickname, phone} = req.body
  await querySql('insert into sys_user(username,password,nickname, phone) value(?,?,?,?)',[username,password,nickname,phone])
  res.send({code:0,msg:'注册成功'})
});


router.post('/page', async(req, res, next) => {
  console.log(req.body)
  let {pageNum,pageSize} = req.body
  let userList = await querySql('select username,password,nickname, phone, create_time createTime from sys_user where del_flag = 0 limit ?,? ',[(pageNum-1)*pageSize, pageSize])
  let total = await querySql('select count(*) as total from sys_user where del_flag = 0')
  res.send({code:0, msg:'成功', rows: userList, total: total[0].total})
});

router.post('/page1', async(req, res, next) => {
  console.log(req.body)
  let {pageNum,pageSize} = req.body
  let userList = await querySql('select username,password,nickname, phone, create_time createTime from sys_user where del_flag = 0 limit ?,? ',[(pageNum-1)*pageSize, pageSize])
  let total = await querySql('select count(*) as total from sys_user where del_flag = 0')
  res.send({code:0, msg:'成功', rows: userList, total: total[0].total})
});




/* 注册接口 */
router.post('/register', async(req, res, next) => {
  let {username,password,nickname} = req.body
  try {
    let user = await querySql('select * from user where username = ?',[username])
    if(!user || user.length === 0){
      password = md5(`${password}${PWD_SALT}`)
      await querySql('insert into user(username,password,nickname) value(?,?,?)',[username,password,nickname])
      res.send({code:0,msg:'注册成功'})
    }else{
      res.send({code:-1,msg:'该账号已注册'})
    }
  }catch(e){
    console.log(e)
    next(e)
  }
});

// 登录接口
router.post('/login',async(req,res,next) => {
  let {username,password} = req.body
  try {
    let user = await querySql('select * from user where username = ?',[username])
      if(!user || user.length === 0){
        res.send({code:-1,msg:'该账号不存在'})
      }else{
        password = md5(`${password}${PWD_SALT}`)
        let result = await querySql('select * from user where username = ? and password = ?',[username,password])
        if(!result || result.length === 0){
          res.send({code:-1,msg:'账号或者密码不正确'})
        }else{
          let token = jwt.sign({username},PRIVATE_KEY,{expiresIn:EXPIRESD})
          res.send({code:0,msg:'登录成功',token:token})
        }
      }
  }catch(e){
    console.log(e)
    next(e)
  }
})

//获取用户信息接口
router.get('/info',async(req,res,next) => {
  let {username} = req.user
  try {
    /*let userinfo = await querySql('select nickname,head_img from user where username = ?',[username])
    res.send({code:0,msg:'成功',data:userinfo[0]})*/
  }catch(e){
    console.log(e)
    next(e)
  }
})

//头像上传接口
/*route.post('/upload',upload.single('head_img'),async(req,res,next) => {
  console.logs(req.file)
  let imgPath = req.file.path.split('public')[1]
  let imgUrl = 'http://127.0.0.1:3000'+imgPath
  res.send({code:0,msg:'上传成功',data:imgUrl})
})

//用户信息更新接口
route.post('/updateUser',async(req,res,next) => {
  let {nickname,head_img} = req.body
  let {username} = req.user
  try {
    let result = await querySql('update user set nickname = ?,head_img = ? where username = ?',[nickname,head_img,username])
    console.logs(result)
    res.send({code:0,msg:'更新成功',data:null})
  }catch(e){
    console.logs(e)
    next(e)
  }
})*/

module.exports = router;
